package openjdk.stream;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.lang.System.out;

public class Peek {
    public static void main(String[] args) {

        final List<String> strings
                = Stream.of("one", "two", "three", "four")
                .peek(e -> out.println("Original Element: " + e))
                .filter(e -> e.length() > 3)
                .peek(e -> out.println("Filtered value: " + e))
                .map(String::toUpperCase)
                .peek(e -> out.println("Mapped value: " + e))
                .collect(Collectors.toList());
        out.println("Final Results: " + strings);

        final List<MutablePerson> people
                = Stream.of(
                new MutablePerson("Fred"),
                new MutablePerson("Wilma"),
                new MutablePerson("Barney"),
                new MutablePerson("Betty"))
                .peek(person -> out.println(person))
                .peek(person -> person.setName(person.getName().toUpperCase()))
                .collect(Collectors.toList());
        out.println("People: " + people);

    }

    static class MutablePerson {
        private String name;

        public MutablePerson(final String newName) {
            name = newName;
        }

        public String getName() {
            return name;
        }

        public void setName(final String newName) {
            name = newName;
        }

        @Override
        public String toString() {
            return name;
        }
    }
}
